home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / utility / isthere.zip / ISTHERE.C next >
C/C++ Source or Header  |  1992-05-02  |  9KB  |  237 lines

  1. /* ******************************************************************** *
  2.  
  3.    ISTHERE 1.00, Copyright (c) 1992, Bob Flanders and Michael Holmes
  4.    Check for free space
  5.  
  6.    Usage: ISTHERE n [d:]
  7.  
  8.    Where: n  is a number for an absolute number of bytes (e.g. 50000)
  9.          is nK to check in K bytes (e.g. 250K)
  10.          is nM to check in M bytes (e.g. 2M)
  11.       d: is the drive to check
  12.  
  13.    DOS Errorlevel: 0 = Yes, at least that much on drive
  14.            1 = No, less space on drive
  15.            2 = Other error (syntax error, drive not ready, etc.)
  16.  
  17.    Example: ISTHERE 12M C:
  18.         Returns 0 if 12 megabytes or more space is availalble
  19.         on drive C:
  20.  
  21.  * ******************************************************************** *
  22.  
  23.    This code is for Borland C++ version 3.0.
  24.  
  25.  * ******************************************************************** */
  26.  
  27.  
  28. #pragma  pack(1)                            /* pack to byte alignment   */
  29. #include <stdio.h>                          /* standard i/o library     */
  30. #include <string.h>                /* string function def's    */
  31. #include <stdlib.h>                /* common types, etc    */
  32. #include <ctype.h>                /* ctype macros        */
  33. #include <dir.h>                /* get working directory    */
  34. #include <dos.h>                            /* DOS rtn definitions      */
  35.  
  36. #define UINT unsigned int                   /* unsigned integer type    */
  37. #define ULONG unsigned long            /* unsigned long type    */
  38. #define NOT !                               /* logical not              */
  39. #define MAX_PATH 79                         /* max path length          */
  40.  
  41.  
  42. /* ******************************************************************** *
  43.  *      routine definitions                                             *
  44.  * ******************************************************************** */
  45.  
  46. void    quit_with(int, char *),             /* quit with error message  */
  47.     interrupt critical_handler(UINT,    /* critical error handler   */
  48.         UINT, UINT, UINT, UINT, UINT,
  49.         UINT, UINT, UINT, UINT, UINT,
  50.         UINT);
  51.  
  52. int    control_break(void);                /* control break handler    */
  53.  
  54. /* ******************************************************************** *
  55.  *      globals                                                         *
  56.  * ******************************************************************** */
  57.  
  58. int     init_drive,                         /* startup drive            */
  59.     rc = 2;                             /* bad parmaeters           */
  60.  
  61. unsigned
  62. long    avail_space,                        /* available bytes on disk  */
  63.     check_for;                /* amount to check for    */
  64.  
  65. char    drive[] = " :",                     /* drive being checked      */
  66.     drive_nbr;                          /* drive nbr (1 = A)        */
  67.  
  68.  
  69. /* ******************************************************************** *
  70.  *      messages and strings                                            *
  71.  * ******************************************************************** */
  72.  
  73. char    copyright[]    = "ISTHERE 1.00 \xfe Copyright (c) 1992, "
  74.              "Bob Flanders and Michael Holmes\n"
  75.              "Check free space on drive\n\n",
  76.     enuf_space[]   = "Yes, there are",
  77.     insuf_space[]  = "No, there are only",
  78.     space_msg[]    = "%s %ld bytes available on drive %s.\n",
  79.     bad_op[]       = "Invalid parameter %s\n",
  80.     stop_here[]    = "\nStopping at user's request\n",
  81.     bad_drive[]    = "Invalid drive specified or drive not ready.\n",
  82.     bad_number[]   = "The number must be n, nK or nM.\n",
  83.     gtzero[]       = "You should check for more than zero bytes.\n",
  84.     help[]         = "Usage:  ISTHERE n [d:]\n\n"
  85.              "Where:  n  is number (n, nK or nM)\n"
  86.              "        d: is the drive to check\n\n";
  87.  
  88.  
  89. /* ******************************************************************** *
  90.  *      structures                                                      *
  91.  * ******************************************************************** */
  92.  
  93. struct diskfree_t freespace;                /* free space structure     */
  94.  
  95.  
  96. /* ******************************************************************** *
  97.  *      main()                                                          *
  98.  * ******************************************************************** */
  99.  
  100. int     main(int  ac,                       /* DOS cmd line token count */
  101.          char *av[])                    /* ..token strings          */
  102. {
  103. char    *p;                                 /* positional parm pointer  */
  104.  
  105. printf(copyright);                          /* give copyright message   */
  106.  
  107. ctrlbrk(control_break);                     /* set up ctrl break and    */
  108. _dos_setvect(0x24, critical_handler);       /* ..DOS critical handlers  */
  109.  
  110. _dos_getdrive((UINT *) &init_drive);        /* get the default drive    */
  111. drive_nbr = init_drive;                     /* ..set up default drive   */
  112. *drive = init_drive + 'A' - 1;              /* ..nbr and ASCII string   */
  113.  
  114. /* ********************* *
  115.  *  validate parameters  *
  116.  * ********************* */
  117.  
  118. if ((ac < 2) || (ac > 3))            /* q. wrong number of parms?*/
  119.     quit_with(1, "");                /* a. yes .. display help    */
  120.  
  121. if (ac == 3)                                /* q. drive given?          */
  122.     {
  123.     p = av[2];                              /* a. yes .. get pointer    */
  124.  
  125.     if ((strlen(p) == 2) && p[1] == ':')    /* q. valid size and shape? */
  126.     *drive = *p;                        /* a. yes .. copy in drive  */
  127.      else
  128.     quit_with(1, bad_drive);            /* else .. give syntax err  */
  129.     }
  130.  
  131. drive[0] = toupper(drive[0]);               /* uppercase search drive   */
  132. drive_nbr = drive[0] - 'A' + 1;             /* get drive nbr from string*/
  133.  
  134. check_for= strtoul(av[1], &p, 10);        /* get space to check for    */
  135.  
  136. if (check_for == 0L)                /* q. check for 0 bytes?    */
  137.     quit_with(0, gtzero);            /* a. yes.. error        */
  138.  
  139. if (*p)                        /* q. last char null?    */
  140.     {                        /* a. no .. check it out    */
  141.     switch (toupper(*p))            /* check for nK or nM    */
  142.     {
  143.     case 0:                    /* q. neither specified?     */
  144.         break;                /* a. yes .. continue    */
  145.  
  146.     case 'K':                /* q. nK specified?        */
  147.         check_for *= 1024L;            /* a. yes .. multiply by 1K    */
  148.         break;                /* .. continue        */
  149.  
  150.     case 'M':                /* q. nM specified?        */
  151.         check_for *= 1024L * 1024L;        /* a. yes .. multiply by meg*/
  152.         break;                /* .. continue        */
  153.  
  154.     default:                /* otherwise .. error!    */
  155.         quit_with(1, bad_number);        /* .. exit w/error        */
  156.     }
  157.     }
  158.  
  159. if (_dos_getdiskfree(drive_nbr, &freespace))/* q. get freespace ok?    */
  160.     quit_with(0, bad_drive);            /* a. no .. bad drive spec'd*/
  161.  
  162. avail_space =                               /* calculate the free space */
  163.     (long) freespace.avail_clusters
  164.     * (long) freespace.bytes_per_sector
  165.     * (long) freespace.sectors_per_cluster;
  166.  
  167. if (avail_space < check_for)            /* q. enough space?        */
  168.     {                            /* a. no .. error        */
  169.     p = insuf_space;                    /* .. select message    */
  170.     rc = 1;                    /* .. and errorlevel    */
  171.     }
  172.  else                                       /* else .. space available    */
  173.     {
  174.     p = enuf_space;                /* .. ok message            */
  175.     rc = 0;                    /* .. and errorlevel    */
  176.     }
  177.  
  178. printf(space_msg, p, avail_space, drive);   /* display the result    */
  179. return rc;                    /* .. and return to DOS    */
  180. }
  181.  
  182.  
  183. /* ******************************************************************** *
  184.  *      quit_with() -- give an error message, then return to DOS        *
  185.  * ******************************************************************** */
  186.  
  187. void    quit_with(int help_flag,            /* give help message        */
  188.           char *msg)                /* string to print          */
  189. {
  190.  
  191. printf(msg);                                /* give error message ..    */
  192.  
  193. if (help_flag)                    /* q. give help?        */
  194.    printf(help);                /* a. yes .. print the help    */
  195.  
  196. exit(rc);                                   /* ..and then quit          */
  197.  
  198. }
  199.  
  200.  
  201. /* ******************************************************************** *
  202.  *      control_break() -- control break intercept routine              *
  203.  * ******************************************************************** */
  204.  
  205. int     control_break(void)
  206. {
  207.  
  208. printf(stop_here);                          /* give error message ..    */
  209.  
  210. return(0);
  211.  
  212. }
  213.  
  214.  
  215.  
  216. /* ******************************************************************** *
  217.  *      critical_handler() -- DOS critical error handler                *
  218.  * ******************************************************************** */
  219.  
  220. #pragma argsused                    /* hold unused argument messages    */
  221. #pragma option -O2-b-e              /* no global register allocation    */
  222.                     /* ..or dead code elimination       */
  223.  
  224. void    interrupt critical_handler(UINT bp,
  225.         UINT di, UINT si, UINT ds, UINT es,
  226.         UINT dx, UINT cx, UINT bx, UINT ax,
  227.         UINT cs, UINT ip, UINT flags)
  228. {
  229.  
  230.  
  231. if (ax & 0x800)                             /* q. fail allowed?         */
  232.    ax = (ax & 0xff00) | 3;                  /* a. yes .. fail request   */
  233. else
  234.    ax = (ax & 0xff00) | 2;                  /* else .. abort request    */
  235.  
  236. }
  237.